package cn.Y_Qiu.org.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.org.domain.Shop;
import cn.Y_Qiu.org.query.ShopQuery;
import cn.Y_Qiu.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    // 添加
    @PutMapping
    public  Ajaxresult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
            } else {
                shopService.update(shop);
            }
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("炸了");
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    public Ajaxresult delById(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("失败");
        }
    }
    // 查单
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }
    // 查全
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.listAll();
    }
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }


    @PatchMapping
    public Ajaxresult patchremove(@RequestBody List<Long> ids){
        System.out.println("到达controller");
        try {
            shopService.patchremove(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("失败");
        }

    }
    @PostMapping("/settlement")
    public Ajaxresult shopRegister(@RequestBody Shop shop){
        try {
            shopService.regist(shop);
            return new Ajaxresult();
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajaxresult().setMessage(e.getMessage());
        }
    }
}
